/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.infonova.education.microservice.service.impl;

import com.infonova.education.microservice.repository.OrderPositionRepository;
import com.infonova.education.microservice.repository.OrderRepository;
import com.infonova.education.microservice.repository.ProductRepository;
import com.infonova.education.microservice.repository.entity.Order;
import com.infonova.education.microservice.repository.entity.OrderPosition;
import com.infonova.education.microservice.repository.entity.Product;
import com.infonova.education.microservice.service.OrderService;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.Set;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Stefan
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private OrderPositionRepository orderPositionRepository;

    @Autowired
    private ProductRepository productRepository;

    @Override
    public long createOrder(long customerId) {
        Order order = new Order(customerId);
        orderRepository.save(order);
        return order.getOrderId();
    }

    @Override
    public long createOrder(Order order) {
        orderRepository.save(order);
        return order.getOrderId();
    }

    @Override
    public void addPosition(OrderPosition position) {
        Order order = orderRepository.findOne(position.getOrderId());
        position.setOrder(order);
        Product product = productRepository.findOne(position.getProductId());
        position.setProduct(product);
        orderPositionRepository.save(position);
    }

    @Override
    public void deletePosition(OrderPosition position) {
        orderPositionRepository.delete(position);
    }

    @Override
    public Order findOrderById(long orderId) {
        return orderRepository.findOne(orderId);
    }

    @Override
    public byte[] printOrderByID(long orderID) throws DocumentException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream);
        fillPdfDocument(document, orderID);
//        document.close();
        byte[] pdfData = byteArrayOutputStream.toByteArray();
        writer.close();
        return pdfData;
    }

    private void fillPdfDocument(Document document, long orderID) throws DocumentException {
        Order order = orderRepository.findOne(orderID);
        Set<OrderPosition> orderPositions = order.getPositions();

        //Properties
        document.addAuthor("Kainflix");
        document.addCreator("Kainflix");
        document.addSubject("Order");
        document.addTitle("Order Summary");

        document.open();
        document.add(new Paragraph("CustomerID: " + order.getCustomerId()));
        document.add(new Paragraph("OrderID: " + order.getOrderId()));
        document.add(new Paragraph("Positions: "));
        List l = new List(true, false, 10);
        l.add(new ListItem(String.format("%s | %s | %s", "Anzahl", "Product", "Preis")));
        
        for (OrderPosition position : orderPositions) {
            l.add(new ListItem(String.format("%d | %s | %d",position.getAnzahl() ,position.getProduct().getName() , position.getProduct().getPrice())));
        }
        document.add(l);
        document.close();
    }

    @Override
    public Iterable<Order> findAll() {
        return orderRepository.findAll();
    }

    @Override
    public void finishOrder(long orderId) {
        orderRepository.delete(orderId);
    }

    @Override
    public OrderPosition findOrderPositionById(Long orderPositinoId) {
        return orderPositionRepository.findOne(orderPositinoId);
    }

    @Override
    public long calculatePrice(long orderId) {
        long price=0;
        Order order = orderRepository.findOne(orderId);
        Set<OrderPosition> positions=order.getPositions();
        for (OrderPosition position : positions) {
            price+=position.getProduct().getPrice()*position.getAnzahl();
        }    return price;
    }

}
